
#include "../server.hpp"
#include <string>
#include <iostream>


class Echoserver{
    private:
        TcpServer _server;
    private:
        void OnConnected(const PtrConnection &conn) {
            DBG_LOG("NEW CONNECTION:%p", conn.get());
        }
        void OnClosed(const PtrConnection &conn) {
            DBG_LOG("CLOSE CONNECTION:%p", conn.get());
        }
        void OnMessage(const PtrConnection &conn, Buffer *buf) {
            conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
            buf->MoveReadOffset(buf->ReadAbleSize());
            conn->Shutdown(); //只是 回显一次
        }
    public:
        Echoserver(uint16_t port = 20000) :_server(port){
            _server.SetThreadCount(3);
            _server.SetConnectedCallback(std::bind(&Echoserver::OnConnected, this, std::placeholders::_1));
            _server.SetMessageCallback(std::bind(&Echoserver::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
            _server.SetClosedCallback(std::bind(&Echoserver::OnClosed, this, std::placeholders::_1));
        }

        void start(){
            _server.Start();
        }


};